package sweetguy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import sweetguy.dto.EmpInsertDTO;
import sweetguy.dto.EmpUpdateDTO;
import sweetguy.dto.LoginDTO;
import sweetguy.dto.PageDTO;
import sweetguy.entity.dto.Emp;
import sweetguy.exception.ServiceException;
import sweetguy.mapper.EmpMapper;
import sweetguy.mapper.PermissionMapper;
import sweetguy.service.EmpService;
import sweetguy.util.JwtUtil;
import sweetguy.vo.LoginVO;

import java.time.LocalDateTime;
import java.util.List;

@Service
@CacheConfig(cacheNames = "emp")
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(EmpInsertDTO dto) {
        Emp emp = BeanUtil.copyProperties(dto, Emp.class);

        // 设置默认值
        String idcard = emp.getIdcard();
        emp.setAvatar("default-avatar.png");
        emp.setGender(IdcardUtil.getGenderByIdCard(idcard));
        emp.setAge(IdcardUtil.getAgeByIdCard(idcard));
        emp.setProvince(IdcardUtil.getProvinceByIdCard(idcard));
        emp.setAddress("暂未添加详细住址");
        emp.setInfo("暂未添加详细介绍");
        emp.setDeleted(0);
        emp.setCreated(LocalDateTime.now());
        emp.setUpdated(LocalDateTime.now());
        int result = empMapper.insert(emp);
        if (result <= 0) {
            throw new ServiceException("DB添加失败");
        }
        return result;
    }

    @Override
    public Emp selectById(Long id) {
        Emp result = empMapper.selectById(id);
        if (ObjectUtil.isNull(result)) {
            throw new ServiceException("ID不存在");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int updateById(EmpUpdateDTO dto) {
        Emp emp = BeanUtil.copyProperties(dto, Emp.class);
        // 设置默认值
        String idcard = emp.getIdcard();
        emp.setAvatar("default-avatar.png");
        emp.setGender(IdcardUtil.getGenderByIdCard(idcard));
        emp.setAge(IdcardUtil.getAgeByIdCard(idcard));
        emp.setProvince(IdcardUtil.getProvinceByIdCard(idcard));
        emp.setAddress("暂未添加详细住址");
        emp.setInfo("暂未添加详细介绍");
        emp.setDeleted(0);
        emp.setCreated(LocalDateTime.now());
        emp.setUpdated(LocalDateTime.now());
        int result = empMapper.updateById(emp);
        if (result <= 0) {
            throw new ServiceException("DB添加失败");
        }
        return result;
    }

    @Override
    public int logicDeleteById(Long id) {
        int result = empMapper.logicDeleteById(id);
        if (result <= 0) {
            throw new ServiceException("DB删除失败");
        }
        return result;
    }

    @Override
    public int logicDeleteByIds(List<Long> ids) {
        int result = empMapper.logicDeleteByIds(ids);
        if (result <= 0) {
            throw new ServiceException("DB逻辑批量删除失败");
        }
        return result;
    }

    @Override
    public List<Emp> list() {
        return empMapper.list();
    }

    @Override
    public PageInfo<Emp> page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return new PageInfo<>(empMapper.list());
    }

    @Override
    public LoginVO loginByAccount(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = SecureUtil.md5(loginDTO.getPassword());
        Emp emp = empMapper.selectByAccount(username,password);
        if (ObjectUtil.isNull(emp)) {
            throw new ServiceException("账号密码有误");
        }
        String token = JwtUtil.build(emp.getId(), emp.getRealname(), emp.getAvatar());
        LoginVO loginVO = new LoginVO();
        loginVO.setToken(token);
        loginVO.setPermissions(permissionMapper.selectByEmpId(emp.getId()));
        loginVO.setEmp(emp);
        return loginVO;
    }
}
